package com.wpi.iCoach;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class is for user to add new service information 
 * which would be inserted into the database. 
 * @author Kenneth J. Loomis (kjloomis)
 * @version June 2012
 */
public class SaveLocation extends Activity {
    
	/**
	 * a button to confirm the information that user add.
	 */
	Button submit;
	//EditText etGPS1 = ( EditText ) findViewById ( R.id.etGPS1 );
	//EditText etGPS2 = ( EditText ) findViewById ( R.id.etGPS2 );
	//EditText etLocName =  ( EditText ) findViewById ( R.id.etLocName );
	/**
	 * User would type the latitude, longitude and name of a new service in the editText view.
	 */
	EditText etGPS1, etGPS2, etLocName;

	/**
	 * They represents the latitude, longitude and name of a new service.
	 */
	float lat, lon;
	String name;
	
	/** Called when the activity is first created. */
	
	/**
	 * This method would be called when this activity is first created.
	 * It would first load its UI component from the saveLocation.xml. 
	 * The information user adds would be inserted into database.
	 * @param savedInstanceState
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.savelocation);
       
        LocationManager mloc = ( LocationManager ) getSystemService ( Context.LOCATION_SERVICE );
        //Criteria crit = new Criteria();
        //Location loc = mloc.getLastKnownLocation( mloc.getBestProvider( crit, false ) );
        Location loc = mloc.getLastKnownLocation ( LocationManager.GPS_PROVIDER );
       
        etGPS1 = ( EditText ) findViewById ( R.id.etGPS1 );
        etGPS2 = ( EditText ) findViewById ( R.id.etGPS2 );
        etLocName =  ( EditText ) findViewById ( R.id.etLocName );

        if ( loc != null )
        {
        	etGPS1.setText( Double.toString( loc.getLatitude() ) );
        	etGPS2.setText( Double.toString( loc.getLongitude() ) );
        }
        else
        {
        	etGPS1.setText( "0.00" );
        	etGPS2.setText( "0.00" );
 			Dialog d = new Dialog ( SaveLocation.this );
			d.setTitle( "Location Not Known!" );
			TextView tv = new TextView ( SaveLocation.this );
			tv.setText( "Your current location is not known. The coordinates are set to the default: (0, 0)." );
			d.setContentView( tv );
			d.show ( ); 
        }
        
        submit = ( Button ) findViewById ( R.id.btnSubmit );
        submit.setOnClickListener( new View.OnClickListener() {
			
        	/**
        	 * This method handles what appropriate activity would be started 
        	 * when any button on the screen is clicked.  
        	 * @param v 
        	 */
			@Override
			public void onClick( View v ) {
				// TODO Auto-generated method stub
				
				boolean inserted = true; 
				lat = Float.parseFloat( etGPS1.getText().toString() );
				lon = Float.parseFloat( etGPS2.getText().toString() );
				name = etLocName.getText().toString();
				
				try 
				{
					LocDBMgr locMngr = new LocDBMgr ( SaveLocation.this );
					locMngr.Open ( );
					locMngr.AddToLocationDB (lat, lon, name );
					locMngr.Close ( );
				}
				catch ( Exception e )
				{
					//Replaced with toast
					/*
					Dialog d = new Dialog ( SaveLocation.this );
					d.setTitle( "Not inserted!" );
					TextView tv = new TextView ( SaveLocation.this );
					tv.setText( "An unknown error occured." );
					d.setContentView( tv );
					d.show ( );
					*/
					
					Toast toast1 = Toast.makeText( SaveLocation.this, 
							  "Not inserted! An unknown error has occurred.", 
							  Toast.LENGTH_SHORT);
					toast1.show();
					
					e.printStackTrace ( );
					inserted = false;
					
				}
				finally
				{
					if ( inserted )
					{
						//Replaced with toast
						/*
						Dialog d = new Dialog ( SaveLocation.this );
						d.setTitle( "Successfully Added!");
						TextView tv = new TextView ( SaveLocation.this );
						tv.setText( "The location has been added to the database. Tap back to close." );
						d.setContentView( tv );
						d.show ( );
						*/
						
						Toast toast2 = Toast.makeText( SaveLocation.this, 
													  "The location has been added to the database. Tap back to close.", 
													  Toast.LENGTH_SHORT);
						toast2.show();
					}
				}
			} 
        });
    }
}